<?php

namespace app\modules\admin\controllers;

use Yii;
use app\modules\admin\models\MemberCard;
use app\modules\admin\models\MemberCardSearch;
use app\modules\admin\components\BaseAdminController;
use app\components\Api;
use app\components\Wechat;
use app\components\Loyalty;
use app\components\Log;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\Tenant;
use yii\data\ArrayDataProvider;


class MemberController extends BaseAdminController
{
    public function init()
    {
        $identifier = $this->session('identifier');
        $tenant = Tenant::find()->where(['identifier' => $identifier])->all();
        if (count($tenant) != 1) {
            exit('Identifier Miss. 没有设定Tenant');
        }
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }


    public function actionIndex()
    {
        $data = [
            'offset' => 0,
            'count' => 10,
            'status_list' => 'CARD_STATUS_VERIFY_OK'
        ];
        $res = Wechat::card_list($data);
        if ($res['errmsg'] == 'ok' && $res['errcode'] == 0) {
            $card_list = $res['card_id_list'];
        }
        $datavalue = [];
        foreach ($card_list as $key => $val) {
            $datavalue[$key] = [
                'id' => $key + 1,
                'card_id' => $val
            ];
        }
        // echo "<pre>";
        // var_dump($datavalue);die;
        $dataProvider = new ArrayDataProvider([
            'allModels' => $datavalue,
            'pagination' => [
                'pageSize' => 10,
            ],
            // 'sort' => [
            //     'attributes' => ['id', 'name'],
            // ],
        ]);
        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    public function actionView($card_id)
    {
        if (!empty($card_id)) {
            $re = Wechat::card_get(['card_id' => $card_id]);
            if ($re['errmsg'] == 'ok') {
                $card = $re['card'];
            } else {
                exit($re['errmsg']);
            }
            echo "<pre>";
            var_dump($re);
            die;
        }
    }
    // 通过API修改会员卡信息
    public function actionUpdate()
    {
        $data = [
            // 'card_id' => 'p2Rmrt3QSQbzKUNYXtE9zgmOysFM',
            // 'card_id' => 'p2Rmrt8P3nizaSt5GsU4pRD0gGU0',
            // 'member_card' => [
                // 'prerogative' => '仅作测试卡使用,不做任何商业用途',
                // 'background_pic_url' => 'https://mmbiz.qpic.cn/mmbiz_png/UzsLSJicrzHfCA319RTQy5gtUEwqyD9AjRdwJjRib8hSO7JictJTeq9d2Hlpgjia71jWFtlW8r2L59vZUXkXmzQ5cg/0',
                // 'base_info' => [
                //     'logo_url' => 'https://mmbiz.qpic.cn/mmbiz_png/UzsLSJicrzHfCA319RTQy5gtUEwqyD9AjAJlukKlsKibRJXnAZnRWiabVtc3L3NibQ7jn0Vs77iazpyIsialPaXPN36Q/0',
                //     // 'brand_name' => 'JSELECT',
                //     // 'code_type' => 'CODE_TYPE_QRCODE',
                //     'title' => '会员卡',
                //     'color' => 'Color030',
                //     'notice' => '使用时向服务员出示此券',
                //     'description' => '不可与其他优惠同享',
                //     'custom_url_name' => '我的资料',
                //     'custom_url' => 'http://winshare.onthemooner.com/index.php/member/user-info?appid=wxeca2e361be1de14c',
                //     'custom_url_sub_title' => '',
                //     'promotion_url_name' => '会员卡详情',
                //     'promotion_url' => 'http://winshare.onthemooner.com/index.php/member/user-card-info?appid=wxeca2e361be1de14c',
                // ],
                // 'auto_activate' => false,
                // 'activate_url' => '',
                // 'wx_activate' => true,
                // 'wx_activate_after_submit' => true,
                // 'wx_activate_after_submit_url' => 'http://winshare.onthemooner.com/index.php/member-binding/new-member?appid=wxeca2e361be1de14c',
            // ],
        ];
        $res = Wechat::card_update($data);
        print_r($res);
        die;

    }
}